package com.neuedu.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.neuedu.entity.UmsStudent;
import com.neuedu.service.UmsStudentService;
import com.neuedu.vo.ResultJson;
import io.minio.errors.*;
import jakarta.annotation.Resource;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.security.InvalidKeyException;
import java.security.NoSuchAlgorithmException;
import java.util.List;

/**
 * <p>
 * 学生表 前端控制器
 * </p>
 *
 * @author wangyu
 * @since 2025-09-03
 */
@RestController
@RequestMapping("/umsStudent")
public class UmsStudentController {
    @Resource
    UmsStudentService umsStudentService;

    @GetMapping("/list")
    ResultJson<IPage<UmsStudent>> list(Integer pageNo, Integer pageSize) {
        return ResultJson.success(umsStudentService.page(new Page<>(pageNo, pageSize)));
    }
    @GetMapping("/listByName")
    ResultJson<List<UmsStudent>> list(String name) {
        return ResultJson.success(umsStudentService.listByName(name));
    }
    @PostMapping("/add")
    ResultJson<Boolean> add(UmsStudent umsStudent) {
        return ResultJson.success(umsStudentService.add(umsStudent), "添加学生成功");
    }

    @GetMapping("/check")
    ResultJson<Boolean> check(String field, String value, Long id) {
        return ResultJson.success(umsStudentService.check(field, value, id));
    }

    @GetMapping("/getById")
    ResultJson<UmsStudent> getById(Long id) {
        return ResultJson.success(umsStudentService.getById(id));
    }

    @PostMapping("/update")
    ResultJson<Boolean> update(UmsStudent umsStudent) {
        return ResultJson.success(umsStudentService.updateById(umsStudent), "修改成功");
    }

    @PostMapping("/delete")
    ResultJson<Boolean> delete(UmsStudent umsStudent) {
        return ResultJson.success(umsStudentService.delete(umsStudent), umsStudent.getActive() ? "该学生已经启用" : "该学生已经禁用");
    }
    @PostMapping("/login")
    ResultJson<String> login(String username, String password) {
        return ResultJson.success(umsStudentService.login(username, password));
    }
    @GetMapping("/getUserIcon")
    ResultJson<UmsStudent> getUserIcon(Long login_id) {
        return ResultJson.success(umsStudentService.getStudent(login_id));
    }
    @GetMapping("/getUserInfo")
    ResultJson<UmsStudent> getUserInfo(Long login_id) {
        return ResultJson.success(umsStudentService.getStudent(login_id));
    }
    @PostMapping("/upload/icon")
    ResultJson<Boolean> uploadIcon(MultipartFile file, Long login_id) throws ServerException, InsufficientDataException, ErrorResponseException, IOException, NoSuchAlgorithmException, InvalidKeyException, InvalidResponseException, XmlParserException, InternalException {
        return ResultJson.success(umsStudentService.uploadIcon(file, login_id), "头像上传成功");
    }
    @PostMapping("/sendVerificationCode")
    ResultJson<Boolean> sendVerificationCode(String email) throws ServerException, InsufficientDataException, ErrorResponseException, IOException, NoSuchAlgorithmException, InvalidKeyException, InvalidResponseException, XmlParserException, InternalException {
        return ResultJson.success(umsStudentService.sendVerificationCode(email));
    }

    @PostMapping("/verifyCode")
    ResultJson<Boolean> verifyCode(String email, String code) {
        return ResultJson.success(umsStudentService.verifyCode(email, code));
    }

    @PostMapping("/resetPassword")
    ResultJson<Boolean> resetPassword(String email, String newPassword) {
        return ResultJson.success(umsStudentService.resetPassword(email, newPassword));
    }
    @PostMapping("/uploadList")
    ResultJson<List<UmsStudent>> uploadList(MultipartFile file) throws IOException {
        return ResultJson.success(umsStudentService.uploadList(file));
    }
    @PostMapping("/saveBatch")
    ResultJson<Boolean> saveBatch(String list) {
        return ResultJson.success(umsStudentService.saveBatch(list), "导入学生信息成功");
    }
}
